package com.dyc.caradvertisting.ui.fmt.login;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dyc.caradvertisting.R;
import com.dyc.caradvertisting.bean.req.ClientLogin;
import com.dyc.caradvertisting.event.StartBrotherEvent;
import com.dyc.caradvertisting.ui.fmt.find.FindPwdMsgFmt;
import com.dyc.caradvertisting.ui.fmt.register.RegisterFmt;
import com.google.gson.Gson;
import com.njh.tools.base.BaseFragment;
import com.njh.tools.common.ToastUtils;
import com.njh.tools.common.string.StringUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.yokeyword.fragmentation.SupportActivity;

/**
 * A simple {@link Fragment} subclass.
 * 用户登录
 */
public class LoginFragment extends BaseFragment<LoginPresenter, LoginModel> implements LoginContract.View {


    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.txt_reg)
    TextView txtReg;
    @BindView(R.id.tv_find_pwd)
    TextView tvFindPwd;
    @BindView(R.id.toolbar_title_tv)
    TextView toolbarTitleTv;
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    public LoginFragment() {
        // Required empty public constructor
    }

    public static LoginFragment newInstance() {
        LoginFragment fragment = new LoginFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ButterKnife.bind(this, getView(inflater, container));
        return getView(inflater, container);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_login;
    }

    @Override
    public void initView(View baseView) {
        ButterKnife.bind(this, baseView);
        initToolbar(R.string.login);
    }

    @Override
    public boolean isSwipeBack() {
        return false;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel, (SupportActivity) this.getBaseActivty());
    }

    @Override
    public void finish() {
        this._mActivity.onBackPressed();
    }


    @OnClick({R.id.btn_login, R.id.txt_reg,R.id.tv_find_pwd})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                String accout = etAccount.getText().toString();
                String pwd = etPassword.getText().toString();
                if (StringUtils.isEmpty(accout)) {
                    ToastUtils.showLong(this.getContext(), "请输入账号");
                    return;
                }
                if (StringUtils.isEmpty(pwd)) {
                    ToastUtils.showLong(this.getContext(), "请输入密码");
                    return;
                }
                showLoading();
                String json = new Gson().toJson(new ClientLogin(accout, pwd));
                mPresenter.reqUserlogin(json);
                break;
            case R.id.txt_reg:
                // 这里我使用EventBus通知父MainFragment处理跳转(接耦),
                EventBus.getDefault().post(new StartBrotherEvent(RegisterFmt.newInstance()));
                break;
            case R.id.tv_find_pwd:
                // 这里我使用EventBus通知父MainFragment处理跳转(接耦),
                EventBus.getDefault().post(new StartBrotherEvent(FindPwdMsgFmt.newInstance()));
                break;
        }
    }
}
